package com.ruoyi.web.controller.digest;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.digest.domain.dto.AnalysisProgressDTO;
import com.ruoyi.digest.domain.vo.AnalysisProgressVO;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.digest.domain.HsGeneratedText;
import com.ruoyi.digest.service.IHsGeneratedTextService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 热搜主题AI生成文本Controller
 * 
 * @author lzk
 * @date 2025-06-16
 */
@RestController
@RequestMapping("/digest/digest")
@Tag(name = "【热搜主题AI生成文本】管理")
@Slf4j
public class HsGeneratedTextController extends BaseController
{
    @Autowired
    private IHsGeneratedTextService hsGeneratedTextService;

    /**
     * 查询热搜主题AI生成文本列表
     */
    @Operation(summary = "查询热搜主题AI生成文本列表")
    @GetMapping("/list")
    public TableDataInfo list(HsGeneratedText hsGeneratedText)
    {
        startPage();
        List<HsGeneratedText> list = hsGeneratedTextService.selectHsGeneratedTextList(hsGeneratedText);
        return getDataTable(list);
    }

    /**
     * 获取热搜主题AI生成文本详细信息
     */
    @Operation(summary = "获取热搜主题AI生成文本详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hsGeneratedTextService.selectHsGeneratedTextById(id));
    }

    /**
     * 新增热搜主题AI生成文本
     */
    @Operation(summary = "新增热搜主题AI生成文本")
    @PreAuthorize("@ss.hasPermi('digest:digest:add')")
    @Log(title = "热搜主题AI生成文本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsGeneratedText hsGeneratedText)
    {
        return toAjax(hsGeneratedTextService.insertHsGeneratedText(hsGeneratedText));
    }

    /**
     * 修改热搜主题AI生成文本
     */
    @Operation(summary = "修改热搜主题AI生成文本")
    @PreAuthorize("@ss.hasPermi('digest:digest:edit')")
    @Log(title = "热搜主题AI生成文本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsGeneratedText hsGeneratedText)
    {
        return toAjax(hsGeneratedTextService.updateHsGeneratedText(hsGeneratedText));
    }

    /**
     * 删除热搜主题AI生成文本
     */
    @Operation(summary = "删除热搜主题AI生成文本")
    @PreAuthorize("@ss.hasPermi('digest:digest:remove')")
    @Log(title = "热搜主题AI生成文本", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable( name = "ids" ) Long[] ids) 
    {
        return toAjax(hsGeneratedTextService.deleteHsGeneratedTextByIds(ids));
    }

    /**
     * 自动分析所有未处理的主题
     *
     * @return 统一响应结果
     */
    @Operation(summary = "自动分析所有未处理的主题")
    @PreAuthorize("@ss.hasPermi('digest:digest:analyze')")
    @Log(title = "热搜主题AI自动分析", businessType = BusinessType.OTHER)
    @PostMapping("/ai/auto-analyze")
    public AjaxResult autoAnalyzeAllTopics() {
        try {
            Map<String, Object> result = hsGeneratedTextService.autoAnalyzeAllTopics();
            if (result.containsKey("error")) {
                return AjaxResult.error(result.get("error").toString());
            }
            return AjaxResult.success(result);
        } catch (Exception e) {
            log.error("自动分析失败", e);
            return AjaxResult.error("自动分析失败: " + e.getMessage());
        }
    }

    /**
     * 查询分析进度
     *
     * @param taskId 任务ID
     * @return 进度信息
     */
    @Operation(summary = "查询分析进度")
    @PreAuthorize("@ss.hasPermi('digest:digest:analyze')")
    @GetMapping("/ai/progress/{taskId}")
    public AjaxResult getAnalysisProgress(@PathVariable @Schema(description = "任务ID") String taskId) {
        try {
            AnalysisProgressDTO dto = new AnalysisProgressDTO(taskId);
            AnalysisProgressVO progress = hsGeneratedTextService.getAnalysisProgressVO(dto);
            return AjaxResult.success(progress);
        } catch (Exception e) {
            log.error("查询分析进度失败: taskId={}", taskId, e);
            return AjaxResult.error("查询进度失败: " + e.getMessage());
        }
    }

    @PostMapping("/ai/stopAnalysis")
    public AjaxResult stopAnalysis(@RequestParam String taskId) {
        try {
            Map<String, Object> result = hsGeneratedTextService.stopAnalysisTask(taskId);
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error("停止分析失败: " + e.getMessage());
        }
    }
}
